package cn.ulc.socket.admin.service.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.activityGoods.ActivityGoodsDTO;
import cn.ulc.socket.admin.model.dto.activityGoods.ActivityGoodsListDTO;
import cn.ulc.socket.admin.model.param.activityGoods.ActivityGoodsParam;
import cn.ulc.socket.admin.service.ActivityGoodsServ;
import cn.ulc.store.dao.ActivityGoodsDao;
import cn.ulc.store.entity.ActivityGoods;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 活动商品
 */
@Service
public class ActivityGoodsServImpl extends BaseService implements ActivityGoodsServ {
    private static final Log log = LogFactory.get();

    @Autowired
    ActivityGoodsDao activityGoodsDao;

    @Override
    public ActivityGoodsListDTO findList(Long activityId,String key) {
        List<ActivityGoods> list = activityGoodsDao.findList(activityId,key);
        return new ActivityGoodsListDTO(list,0l);
    }

    @Override
    public ActivityGoodsDTO findDetail(Long id) {
        ActivityGoods entity = activityGoodsDao.findOne(id);
        return new ActivityGoodsDTO(entity);
    }

    @Override
    @Transactional
    public Boolean saveAll(ActivityGoodsParam param) {
        if(param.getIds().size()>0){
            List<ActivityGoods> list = new ArrayList<>();
            for (Long id : param.getIds()) {
                ActivityGoods entity = new ActivityGoods();
                setBaseInfo(entity);
                entity.setActivityId(param.getActivityId());
                entity.setGoodsId(id);
                entity.setSort(1);
                list.add(entity);
            }
            activityGoodsDao.saveAll(list);
        }
        return true;
    }

    @Override
    public Long deleteOne(Long id) {
        ActivityGoods entity = activityGoodsDao.findOne(id);
        if(entity != null) {
            entity.setDeleted(DeletedStatus.DELETED);
            setBaseInfo(entity);
            activityGoodsDao.updateTemplateById(entity);
        }
        return id;
    }
}
